//-----------------------------------------------------------------------
// <copyright file="tblExchangeRateDAOBase.cs" company="Scb">
//     Copyright ? Scb. All rights reserved.
// </copyright>
//-----------------------------------------------------------------------

using System;
using System.Data;
using System.Data.Common;
using Scb.Framework;
using Scb.Framework.Data;
using Microsoft.Practices.EnterpriseLibrary.Data;
using Robot.Entities;

namespace Robot.Data
{    
    public abstract class tblExchangeRateDAOBase
    {
        #region Common methods
        public virtual tblExchangeRate CreatetblExchangeRateFromReader(IDataReader reader)
        {
            tblExchangeRate item = new tblExchangeRate();
            try
            {
                if (UtilityDAO.CheckColumnData(reader,"ID")) item.ID = (Guid)reader["ID"];
                if (UtilityDAO.CheckColumnData(reader,"BuyCCY")) item.BuyCCY = (string)reader["BuyCCY"];
                if (UtilityDAO.CheckColumnData(reader,"BuyRate")) item.BuyRate = (decimal)reader["BuyRate"];
                if (UtilityDAO.CheckColumnData(reader,"SellCCY")) item.SellCCY = (string)reader["SellCCY"];
                if (UtilityDAO.CheckColumnData(reader,"SellRate")) item.SellRate = (decimal)reader["SellRate"];
                if (UtilityDAO.CheckColumnData(reader,"FromFileID")) item.FromFileID = (Guid)reader["FromFileID"];
                if (UtilityDAO.CheckColumnData(reader,"CreatedDate")) item.CreatedDate = (DateTime)reader["CreatedDate"];
                if (UtilityDAO.CheckColumnData(reader,"CreatedBy")) item.CreatedBy = (string)reader["CreatedBy"];
            }
            catch (Exception ex)
            {
                // log this exception
                Logger.Error(ex);
                // wrap it and rethrow
                throw new DataAccessException(SR.DataAccessCreatetblExchangeRateFromReaderException, ex);
            }
            return item;
        }
        #endregion
        
        #region CreatetblExchangeRate methods
            
        public virtual void CreatetblExchangeRate(tblExchangeRate tblExchangeRate)
        {
            try
            {
                Database database = DatabaseFactory.CreateDatabase();
                DbCommand dbCommand = database.GetStoredProcCommand("sptblExchangeRateInsert");
                
                database.AddInParameter(dbCommand, "@BuyCCY", DbType.AnsiString, tblExchangeRate.BuyCCY);
                database.AddInParameter(dbCommand, "@BuyRate", DbType.Decimal, tblExchangeRate.BuyRate);
                database.AddInParameter(dbCommand, "@SellCCY", DbType.AnsiString, tblExchangeRate.SellCCY);
                database.AddInParameter(dbCommand, "@SellRate", DbType.Decimal, tblExchangeRate.SellRate);
                database.AddInParameter(dbCommand, "@FromFileID", DbType.Guid, tblExchangeRate.FromFileID);
                database.AddInParameter(dbCommand, "@CreatedDate", DbType.DateTime, tblExchangeRate.CreatedDate);
                database.AddInParameter(dbCommand, "@CreatedBy", DbType.AnsiString, tblExchangeRate.CreatedBy);
                database.AddOutParameter(dbCommand, "@ID", DbType.Guid, 16);
                
                DataAccessHelper.ExecuteNonQuery(database, dbCommand);
                tblExchangeRate.ID = (Guid)database.GetParameterValue(dbCommand, "@ID");
            }
            catch (Exception ex)
            {
                // log this exception
                Logger.Error(ex);
                // wrap it and rethrow
                throw new DataAccessException(SR.DataAccessCreatetblExchangeRateException, ex);
            }
        }

        #endregion

        #region UpdatetblExchangeRate methods
        
        public virtual void UpdatetblExchangeRate(tblExchangeRate tblExchangeRate)
        {
            try
            {
                Database database = DatabaseFactory.CreateDatabase();
                DbCommand dbCommand = database.GetStoredProcCommand("sptblExchangeRateUpdate");            
                
                database.AddInParameter(dbCommand, "@ID", DbType.Guid, tblExchangeRate.ID);
                database.AddInParameter(dbCommand, "@BuyCCY", DbType.AnsiString, tblExchangeRate.BuyCCY);
                database.AddInParameter(dbCommand, "@BuyRate", DbType.Decimal, tblExchangeRate.BuyRate);
                database.AddInParameter(dbCommand, "@SellCCY", DbType.AnsiString, tblExchangeRate.SellCCY);
                database.AddInParameter(dbCommand, "@SellRate", DbType.Decimal, tblExchangeRate.SellRate);
                database.AddInParameter(dbCommand, "@FromFileID", DbType.Guid, tblExchangeRate.FromFileID);
                database.AddInParameter(dbCommand, "@CreatedDate", DbType.DateTime, tblExchangeRate.CreatedDate);
                database.AddInParameter(dbCommand, "@CreatedBy", DbType.AnsiString, tblExchangeRate.CreatedBy);
                
                DataAccessHelper.ExecuteNonQuery(database, dbCommand);
            }
            catch (Exception ex)
            {
                // log this exception
                Logger.Error(ex);
                // wrap it and rethrow
                throw new DataAccessException(SR.DataAccessUpdatetblExchangeRateException, ex);
            }
        }
        
        #endregion

        #region DeletetblExchangeRate methods
        public virtual void DeletetblExchangeRate(Guid id)
        {
            try
            {
                Database database = DatabaseFactory.CreateDatabase();
                DbCommand dbCommand = database.GetStoredProcCommand("sptblExchangeRateDelete");
                
                database.AddInParameter(dbCommand, "@ID", DbType.Guid, id);
                
                DataAccessHelper.ExecuteNonQuery(database, dbCommand);
            }
            catch (Exception ex)
            {
                // log this exception
                Logger.Error(ex);
                // wrap it and rethrow
                throw new DataAccessException(SR.DataAccessDeletetblExchangeRateException, ex);
            }
        }

        #endregion

        #region GettblExchangeRate methods
        
        public virtual tblExchangeRate GettblExchangeRate(Guid id)
        {            
            try
            {
                Database database = DatabaseFactory.CreateDatabase();
                DbCommand dbCommand = database.GetStoredProcCommand("sptblExchangeRateGet");
                
                database.AddInParameter(dbCommand, "@ID", DbType.Guid, id);
                
                tblExchangeRate tblExchangeRate =  null;
                using (IDataReader reader = DataAccessHelper.ExecuteReader(database, dbCommand))
                {
                    if (reader.Read())
                    {
                        tblExchangeRate = CreatetblExchangeRateFromReader(reader);
                        reader.Close();
                    }
                }
                return tblExchangeRate;
            }
            catch (Exception ex)
            {
                // log this exception
                Logger.Error(ex);
                // wrap it and rethrow
                throw new DataAccessException(SR.DataAccessGettblExchangeRateException, ex);
            }
        }
        
        #endregion

        #region GettblExchangeRateList methods
        public virtual tblExchangeRateCollection GettblExchangeRateList(tblExchangeRateColumns orderBy, OrderDirection orderDirection, int page, int pageSize, out int totalRecords)
        {            
            try
            {
                Database database = DatabaseFactory.CreateDatabase();
                DbCommand dbCommand = database.GetStoredProcCommand("sptblExchangeRateGetList");
                
                database.AddInParameter(dbCommand, "@OrderBy", DbType.AnsiString, orderBy.ToString());
                database.AddInParameter(dbCommand, "@OrderDirection", DbType.AnsiString, orderDirection.ToString());
                database.AddInParameter(dbCommand, "@Page", DbType.Int32, page);
                database.AddInParameter(dbCommand, "@PageSize", DbType.Int32, pageSize);
                database.AddOutParameter(dbCommand, "@TotalRecords", DbType.Int32, 4);
                
                tblExchangeRateCollection tblExchangeRateCollection = new tblExchangeRateCollection();
                using (IDataReader reader = DataAccessHelper.ExecuteReader(database, dbCommand))
                {
                    while (reader.Read())
                    {
                        tblExchangeRate tblExchangeRate = CreatetblExchangeRateFromReader(reader);
                        tblExchangeRateCollection.Add(tblExchangeRate);
                    }
                    reader.Close();
                }
                totalRecords = (int)database.GetParameterValue(dbCommand, "@TotalRecords");
                return tblExchangeRateCollection;
            }
            catch (Exception ex)
            {
                // log this exception
                Logger.Error(ex);
                // wrap it and rethrow
                throw new DataAccessException(SR.DataAccessGettblExchangeRateListException, ex);
            }
        }
        
        public virtual tblExchangeRateCollection GettblExchangeRateList(tblExchangeRateColumns orderBy, OrderDirection orderDirection)
        {            
            int totalRecords = 0;
            return GettblExchangeRateList(orderBy, orderDirection, 0, 0, out totalRecords);
        }
        
        #endregion
    }
}